package com.atguigu.controller;


import com.atguigu.entity.TOrder;
import com.atguigu.entity.TOrderDetail;
import com.atguigu.mapper.OrderInfoMapper;
import com.atguigu.mapper.TOrder1Mapper;
import com.atguigu.service.TOrder1Service;
import com.atguigu.service.TOrderDetail1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-09-14
 */
@RestController
@RequestMapping("/order")
public class TOrder1Controller {

    @Autowired
    private TOrder1Service tOrder1Service;
    @Autowired
    private TOrderDetail1Service tOrderDetail1Service;
    @Autowired
    private TOrder1Mapper tOrder1Mapper;

    //保存订单与分库分表
    @GetMapping("test01")
    public void test01(){
        for (int i = 0; i < 10; i++) {
            TOrder tOrder = new TOrder();
            tOrder.setOrderPrice(99);
            String uuid = UUID.randomUUID().toString();
            tOrder.setTradeNo(uuid);
            tOrder.setOrderStatus("未支付");
            //设置分库分表的依据
            int userId = new Random().nextInt(20);
            System.out.println("userId ===================== " + userId);
            tOrder.setUserId(Long.parseLong(userId+""));
            tOrder1Service.save(tOrder);
        }
    }

    //保存订单与订单详情
    @GetMapping("test02")
    public void test02(){
        for (int i = 0; i < 10; i++) {
            TOrder tOrder = new TOrder();
            tOrder.setTradeNo("0317");
            tOrder.setOrderPrice(9888);
            tOrder.setUserId(3L);
            tOrder1Service.save(tOrder);
            TOrderDetail iphone13 = new TOrderDetail();
            iphone13.setOrderId(tOrder.getId());
            iphone13.setSkuName("Iphone13");
            iphone13.setSkuNum(1);
            iphone13.setSkuPrice(6999);
            iphone13.setUserId(3L);
            tOrderDetail1Service.save(iphone13);

            TOrderDetail sanxing = new TOrderDetail();
            iphone13.setOrderId(tOrder.getId());
            iphone13.setSkuName("三星");
            iphone13.setSkuNum(2);
            iphone13.setSkuPrice(4999);
            iphone13.setUserId(3L);
            tOrderDetail1Service.save(sanxing);

        }
    }
    //查询订单与详情
    @GetMapping("test03")
    public void test03(){

        //List<TOrder>orderList=tOrder1Mapper.queryOrderByCondition(3L);
        List<TOrder>orderList=tOrder1Mapper.queryOrderByCondition(null,1570367039522938881L);
        for (TOrder tOrder : orderList) {
            System.out.println("tOrder = " + tOrder);
        }
    }

}

